/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.message;

import com.jnetx.javax.sip.Messages;
import junit.framework.TestCase;

import javax.sip.header.AcceptHeader;
import java.text.ParseException;
import java.util.ListIterator;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SIPRequestParserTest extends TestCase {
    public SIPRequestParserTest(String name) {
        super(name);
    }

    public void testFullParsing() {
        try {
            SIPRequest r = SIPRequestParser.parseRequest(Messages.INVITE1.getBytes());
            System.out.println("Request:\n" + r);
        } catch (ParseException e) {
            fail("Cannot parse: " + e.getMessage());
        }
    }

    public void testSimpleParsing() {
        final String data = "INVITE sip:bob@biloxi.com SIP/2.0\r\n" +
                "Accept: application/sdp, runtime/rtsp\r\n" +
                "\r\n";
        SIPRequest r;
        try {
            r = SIPRequestParser.parseRequest(data.getBytes());
            System.out.println("Request:\n" + r);
        } catch (ParseException e) {
            fail("Cannot parse: " + e.getMessage());
            return;
        }
        assertNotNull("Accept is null!", r.getHeader(AcceptHeader.NAME));
        ListIterator accepts = r.getHeaders(AcceptHeader.NAME);
        assertNotNull("Accepts are null!", accepts);
        int acceptCounter = 0;
        final String[] contentTypes = {"application", "runtime"};
        final String[] contentSubTypes = {"sdp", "rtsp"};
        while (accepts.hasNext()) {
            AcceptHeader header = (AcceptHeader) accepts.next();
            assertNotNull("Header is null!", header);
            assertEquals("Content type is wrong!", contentTypes[acceptCounter], header.getContentType());
            assertEquals("Content subtype is wrong!", contentSubTypes[acceptCounter], header.getContentSubType());
            acceptCounter++;
        }
        assertEquals("The number of headers is wrong!", 2, acceptCounter);
        System.out.println("\nParsed request:\n" + r);
    }
}
/*
 * $Log$
 */